import { getUserApi } from "@/api/sunnyuser";
import type { SunnyUser } from "@/types/sunnyUser/type";
import { defineStore } from "pinia";
import { ref, computed } from "vue";

export const useUserStore = defineStore("user", () => {
  const sunnyUser = ref<SunnyUser>({} as SunnyUser);

  function getUser() {
    return getUserApi().then(({ data }) => {
      sunnyUser.value = data;
    });
  }

  const avatar = computed(() => {
    //如果获取到了avatar信息，那么用avatar的地址作为url地址
    if (sunnyUser.value.avatar) {
      return `http://192.168.95.138/sunny/sunnyUser/avatar/${
        sunnyUser.value.id
      }?t=${Date.now()}`;
    } else {
      //否则用本地的图片作为url地址
      return new URL("@/assets/img/logo_small.png", import.meta.url).href;
    }
  });
  return { sunnyUser, getUser, avatar };
});
